package org.example.RobotModel.generator.Server;

import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.example.RobotModel.robotDsl.Action;
import org.example.RobotModel.robotDsl.Device;
import org.example.RobotModel.robotDsl.Param;

@SuppressWarnings("all")
public class ServerGen {
  public static CharSequence generateServerRessources(final String packageName, final Action action) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package Server.ServerRessources.");
    _builder.append(packageName, "");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("import org.restlet.resource.ServerResource;");
    _builder.newLine();
    _builder.append("import org.restlet.resource.Post;");
    _builder.newLine();
    _builder.append("import org.restlet.representation.Representation;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import org.json.JSONException;");
    _builder.newLine();
    _builder.append("import org.json.JSONObject;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("/* Resource for one device.");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* ");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("public class ");
    String _name = action.getName();
    _builder.append(_name, "");
    _builder.append(" extends ServerResource {");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.newLine();
    _builder.append("    ");
    _builder.append("@Post(\"json\")");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public String execute(Representation entity) {");
    _builder.newLine();
    _builder.append("    \t");
    _builder.append("try {");
    _builder.newLine();
    _builder.append("    \t\t");
    _builder.append("String req = entity.getText();");
    _builder.newLine();
    _builder.append("    \t\t");
    _builder.append("JSONObject json = new JSONObject(req);");
    _builder.newLine();
    _builder.append("    \t\t");
    _builder.newLine();
    {
      EList<Param> _params = action.getParams();
      for(final Param p : _params) {
        _builder.append("    \t\t");
        String _type = p.getType();
        _builder.append(_type, "    \t\t");
        _builder.append(" ");
        String _name_1 = p.getName();
        _builder.append(_name_1, "    \t\t");
        _builder.append(" = (");
        String _type_1 = p.getType();
        _builder.append(_type_1, "    \t\t");
        _builder.append(") json.get(\"");
        String _name_2 = p.getName();
        _builder.append(_name_2, "    \t\t");
        _builder.append("\");");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.append("    \t\t");
    _builder.newLine();
    _builder.append("    \t");
    _builder.append("}catch(Exception e){");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("e.printStackTrace();");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("    \t");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("return \"");
    String _name_3 = action.getName();
    _builder.append(_name_3, "        ");
    _builder.append("\";");
    _builder.newLineIfNotEmpty();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
  
  public static CharSequence generateServerApplication(final Iterable<Device> ds) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package Server.Application;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import org.restlet.Application;");
    _builder.newLine();
    _builder.append("import org.restlet.Restlet;");
    _builder.newLine();
    _builder.append("import org.restlet.routing.Router;");
    _builder.newLine();
    _builder.newLine();
    {
      for(final Device d : ds) {
        _builder.append("import Server.ServerRessources.");
        String _name = d.getName();
        _builder.append(_name, "");
        _builder.append(".*;");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.newLine();
    _builder.newLine();
    _builder.newLine();
    _builder.append("public class RobotApplication extends Application {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("    ");
    _builder.append("/**");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("* Creates a root Restlet that will receive all incoming calls.");
    _builder.newLine();
    _builder.append("     ");
    _builder.append("*/");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("@Override");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public synchronized Restlet createInboundRoot() {");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("// Create a router Restlet that routes each call to a");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("// new instance of HelloWorldResource.");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("Router router = new Router(getContext());");
    _builder.newLine();
    _builder.newLine();
    _builder.append("        ");
    _builder.append("// Defines routes");
    _builder.newLine();
    {
      for(final Device d_1 : ds) {
        _builder.append("     \t");
        _builder.append("//Routes for device ");
        String _name_1 = d_1.getName();
        _builder.append(_name_1, "     \t");
        _builder.newLineIfNotEmpty();
        {
          EList<Action> _actions = d_1.getActions();
          for(final Action a : _actions) {
            _builder.append("     \t");
            _builder.append("router.attach(\"/");
            String _name_2 = d_1.getName();
            _builder.append(_name_2, "     \t");
            _builder.append("/");
            String _name_3 = a.getName();
            _builder.append(_name_3, "     \t");
            _builder.append("\", Server.ServerRessources.");
            String _name_4 = d_1.getName();
            _builder.append(_name_4, "     \t");
            _builder.append(".");
            String _name_5 = a.getName();
            _builder.append(_name_5, "     \t");
            _builder.append(".class);");
            _builder.newLineIfNotEmpty();
          }
        }
      }
    }
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("return router;");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
  
  public static CharSequence generateServerMain() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package Server;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import org.restlet.Component;");
    _builder.newLine();
    _builder.append("import org.restlet.data.Protocol;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import Server.Application.RobotApplication;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("public class RobotMain {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("    ");
    _builder.append("public static void main(String[] args) {");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("try {");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("// Create a new Component.");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("Component component = new Component();");
    _builder.newLine();
    _builder.newLine();
    _builder.append("            ");
    _builder.append("// Add a new HTTP server listening on port 8182.");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("component.getServers().add(Protocol.HTTP, 8182);");
    _builder.newLine();
    _builder.newLine();
    _builder.append("            ");
    _builder.append("// Attach the sample application.");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("component.getDefaultHost().attach(new RobotApplication());");
    _builder.newLine();
    _builder.newLine();
    _builder.append("            ");
    _builder.append("// Start the component.");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("component.start();");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("} catch (Exception e) {");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("// Something is wrong.");
    _builder.newLine();
    _builder.append("            ");
    _builder.append("e.printStackTrace();");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
}
